package jgl.vajra.kingkong.part.home.activity;

import android.text.TextUtils;

import org.greenrobot.eventbus.EventBus;

import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.bean.CityChoiceBean;
import jgl.vajra.kingkong.databinding.ActivityCitySearchBinding;
import jgl.vajra.kingkong.event.CitySearchEvent;
import jgl.vajra.kingkong.part.home.viewModel.CitySearchViewModel;
import jgl.vajra.kingkong.utils.RandomUtils;
import jgl.vajra.kingkong.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.kingkong.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.kingkong.widget.interfaces.RefreshRecyclerLoadStatusListener;
import jgl.vajra.kingkong.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.kingkong.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.kingkong.widget.recyclerView.RefreshRecyclerNetConfig;
import jgl.vajra.kingkong.widget.retrofithelper.utils.AEACCoder;

@CreateViewModel(CitySearchViewModel.class)
public class CitySearchActivity extends BaseMVVMActivity<CitySearchViewModel, ActivityCitySearchBinding>implements BaseBindingItemPresenter<CityChoiceBean> {

    private String whereEnd;

    @Override
    public int getLayoutId() {
        return R.layout.activity_city_search;
    }

    @Override
    public void initView() {
        super.initView();

        mBinding.etText.setOnKeyListener((view, keyCode, keyEvent) -> {
            if (keyCode == keyEvent.KEYCODE_ENTER) {
                String etText = mBinding.etText.getText().toString().trim();
                if (!TextUtils.isEmpty(etText)) {
                    whereEnd = etText;
                    mBinding.recyclerView.firstLoad();
                }

            }

            return false;
        });
        mBinding.recyclerView.setRefreshRecyclerNetConfig(new RefreshRecyclerNetConfig() {
            @Override
            public Observable getNetObservable(Map<String, Object> map, int action) {
                map.put("Authorization", AEACCoder.encrypt(RandomUtils.getRandom()));
                map.put("whereEnd", URLEncoder.encode(whereEnd));
                return mViewModel.getAreaInfoData(map);
            }
        });

        SingleTypeBindingAdapter adapter = new SingleTypeBindingAdapter(this, null, R.layout.item_city_search);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);

        mBinding.recyclerView.setIsDataObject(true);
        mBinding.recyclerView.setRefreshLoaderMore(false, false);
        mBinding.recyclerView.setRefreshRecyclerLoadStatusListener(new RefreshRecyclerLoadStatusListener<List<CityChoiceBean>>() {
            @Override
            public void onSucceed(List<CityChoiceBean> data, int action) {
                mBinding.recyclerView.setRefreshLoaderMore(false, false);

                mBinding.recyclerView.requestNetObjectSuccess(data, action);
            }

            @Override
            public void onError(String errorInfo, int errorCode, int action) {
                super.onError(errorInfo, errorCode, action);
//                mBinding.recyclerView.requestNetError(errorInfo, errorCode);
            }
        });

    }

    @Override
    public void onItemClick(int position, CityChoiceBean itemData) {
        EventBus.getDefault().post(new CitySearchEvent(itemData));
        finish();
    }
}
